#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
    int f=0,t=0;
    char c=getchar();
    while(!isdigit(c)) t|=(c=='-'),c=getchar();
    while(isdigit(c)) f=(f<<3)+(f<<1)+c-48,c=getchar();
    return t?-f:f;
}
void write(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar('0'+x%10);
}
struct f{int x,y;}a[100005];
bool cmp(f x,f y){return x.y<y.y;}
int sum[100005],sum1[100005];
signed main(){
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	int n=read();
	for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
	sort(a+1,a+1+n,cmp);
	sum[n]=a[n].x+a[n].y;sum1[n]=1e18;
	for(int i=n-1;i>=1;i--){
		if(sum[i+1]>a[i].x+a[i].y){sum1[i]=sum[i+1];sum[i]=a[i].x+a[i].y;}
		else{
			sum[i]=sum[i+1];
			sum1[i]=min(sum1[i+1],a[i].x+a[i].y);
		}
	}
	int mi=1e18,mi1=1e18,ans=1e18;
	for(int i=1;i<=n;i++){
		if(i==1){ans=min(ans,a[i].x+sum[i+1]+sum1[i+1]);}
		else if(i==2){ans=min(ans,min(mi+a[i].x+a[i].y+sum[i+1],a[i].x+sum[i+1]+sum1[i+1]));}
		else if(i==n-1){ans=min(ans,min(mi+mi1+a[i].x+a[i].y*2,a[i].x+sum[i+1]+mi+a[i].y));}
		else if(i==n){ans=min(ans,mi+mi1+a[i].x+a[i].y*2);}
		else{
			ans=min(ans,min(mi+mi1+a[i].x+a[i].y*2,min(mi+sum[i+1]+a[i].x+a[i].y,sum[i+1]+sum1[i+1]+a[i].x)));
		}
		if(mi<a[i].x) mi1=mi,mi=a[i].x;
		else mi1=min(mi1,a[i].x);
//		cout<<ans;
	}
	write(ans);
	return 0;
}
